// COMMAND
//logMessage(LOGLEVEL_DEBUG, "COMMAND", "", "");
char COMMANDcommand[50];
char COMMANDvalue[50];
int COMMANDsource;

/*************************************************************
 *
 * globals
 *
 *************************************************************/
void readCommand() {
  char* tempSerialRead  = _readSerial();    
  if (tempSerialRead) {
    _splitCommandValue(&tempSerialRead);
    executeCommands();
  }
}

void setupCommand() {
  _clearInputMessage();
}


/*************************************************************
 *
 * intern
 *
 *************************************************************/
char* _readSerial() {
  int i = 0;
  char tempMessage[MAX_COMMAND_SIZE];
  if (Serial.available()) {
    //Serial.readBytesUntil('\n', tempMessage, MAX_INPUTMESSAGE_SIZE);
    while (Serial.available() > 0 && i < MAX_INPUTMESSAGE_SIZE) {      
      tempMessage[i++] = Serial.read();
    }    
  }  
  tempMessage[i] = '\0';
  _setCommand(tempMessage);
  return tempMessage;
}

void executeCommands() {
  if (_isCommand("HELP")) {
      _printCommandOverview();
  }  
  else if (_isCommand("SETTIME")) {
      setTime(_getValue());
  }  
  else if (_isCommand("SYNCTIME")) {
      syncTime();
  }   
  else if (_isCommand("LIGHTON")) {
	  if (_equalsValue("1")) {
                  setGivenValue(1,1);
	  }
	  if (_equalsValue("2")) {
                  setGivenValue(2,1);
	  }
  }  
  else if (_isCommand("LIGHTOFF")) {
	  if (_equalsValue("1")) {
		  setGivenValue(1,0);
	  }
	  if (_equalsValue("2")) {
		  setGivenValue(2,0);
	  }
  }
  #ifdef STORAGE_ENABLE
  else if (_isCommand("RESETCOUNTER")) {
      resetCounter();
  }   
  else if (_isCommand("INITSTORAGE")) {
	  initStorage();
  }
  #endif   
  #ifdef SHT15_ENABLE 
  else if (_isCommand("t")) {
      printSerialTemperatureAndHumidity();
  }   
  #endif  
  #ifdef TIME_ENABLE
  else if (_isCommand("c")) {
      printTime();
  }      
  #endif
  else if (_getCommand()[0] != '\0') {    
    Serial.print("Unbekannter Befehl: '");   
    Serial.print(_getCommand());           
    Serial.println("'");       
  }
 
  _clearInputMessage();  
}

void _printCommandOverview() {
  String tempSeparator = "\n";
  String tempHelp = "";
}

/*************************************** 
 * 
 * helpers
 *
 ***************************************/
 
boolean _isCommand(String aCommand) {
  if (aCommand.equals(_getCommand())) {
    return true;
  }
  return false;
}

boolean _equalsValue(String aValue) {
  if (aValue.equals(_getValue())) {
    return true;
  }
  return false;
}

char* _getCommand() {
  return COMMANDcommand;
}

void _setCommand(char* aCommand) {
  strcpy(COMMANDcommand,aCommand);
}

char* _getValue() {
  return COMMANDvalue;
}

void _setValue(char aValue[MAX_INPUTMESSAGE_SIZE]) {
  strcpy(COMMANDvalue,aValue);
}

int _getCommandSource() {
  return COMMANDsource;
}

void _setCommandSource(int aSource) {
  COMMANDsource = aSource;           
}

void _splitCommandValue(char* *aSerialRead) {    
  int tempPos = ((String)*aSerialRead).indexOf('=');

  char tempCommand[MAX_COMMAND_SIZE];
  char tempValue[MAX_COMMAND_SIZE];

  ((String)*aSerialRead).substring(0,tempPos).toCharArray(tempCommand, MAX_COMMAND_SIZE);  
  ((String)*aSerialRead).substring(tempPos + 1).toCharArray(tempValue, MAX_COMMAND_SIZE);  
  
  _setCommand(tempCommand);
  _setValue(tempValue);
}

void _clearInputMessage() {
  char tempRe_setCommand[MAX_COMMAND_SIZE];
  for (int i = 0; i < MAX_COMMAND_SIZE; i++) {
    tempRe_setCommand[i] = '\0';
  }
  _setCommand(tempRe_setCommand);
}
